package com.lej.my.space

import com.alibaba.android.arouter.facade.annotation.Route
import com.lej.core.base.BaseFragment
import com.lej.core.router.CRouter
import com.lej.core.utils.extention.*
import com.lej.my.BR
import com.lej.my.R
import com.lej.my.collect.LoadError
import com.lej.my.collect.LoadLoading
import com.lej.my.collect.LoadSuccess
import com.lej.my.databinding.MyFragmentSpaceBinding

/**
 *  author: cjq
 *  date  : 2022/6/9 0009
 *  desc  :
 */
@Route(path = CRouter.mySpace)
class MySpaceFragment : BaseFragment<MyFragmentSpaceBinding, MySpaceViewModel>() {
    private lateinit var adapter: MySpaceAdapter
    override fun getLayout(): Int = R.layout.my_fragment_space

    override fun initVariableId(): Int = BR.vm

    override fun initData() {
        super.initData()
        requestData()
        adapter = MySpaceAdapter(R.layout.my_fragment_item_space)
        binding.recyclerView.adapter = adapter
    }

    private fun requestData() {
        viewModel.getSpaceInfo {
            adapter.setList(it)
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()

        viewModel.spaceInfo.observe(this) {
            adapter.setList(it)
        }
        viewModel.loadingState.observe(this) {
            when (it) {
                LoadLoading -> {
                    binding.flContainer.showPageStateLoading()
                }
                LoadSuccess -> {
                    binding.flContainer.restorePageState()
                }
                LoadError -> {
                    binding.flContainer.showPageState(PageState.Error("啊哦 网络异常～") { itt ->
                        itt.clickLaunch {
                            requestData()
                        }
                    })
                }
            }
        }
    }
}